package com.starhub.application.collection.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.starhub.application.collection.client.CollectionClientInterface;
import com.starhub.application.collection.dto.DataDefinitionDto;
import com.starhub.application.collection.dto.DataDefinitionLinkDto;
import com.starhub.application.collection.entity.CollectionMetaLink;
import com.starhub.application.collection.entity.KnowledgeCollection;
import com.starhub.application.collection.enums.CollectionTypeEnums;
import com.starhub.application.collection.mapper.CollectionMetaLinkMapper;
import com.starhub.application.collection.mapper.KnowledgeCollectionMapper;
import com.starhub.application.collection.service.CollectionService;
import com.starhub.application.collection.service.DataDefinitionService;
import com.starhub.engine.generator.genid.IdGenerator;
import com.starhub.utils.HubBeanUtils;

import lombok.extern.slf4j.Slf4j;

/**
 * 数据定义服务实现类
 * 
 * @version: V1.0
 * @author: liuhf
 * @time 2025-06-06
 */
@Slf4j
@Service
public class DataDefinitionServiceImpl extends CollectionService implements DataDefinitionService {

    private final CollectionMetaLinkMapper collectionMetaLinkMapper;
    private final KnowledgeCollectionMapper knowledgeCollectionMapper;

    public DataDefinitionServiceImpl(CollectionMetaLinkMapper collectionMetaLinkMapper,
            KnowledgeCollectionMapper knowledgeCollectionMapper) {
        this.collectionMetaLinkMapper = collectionMetaLinkMapper;
        this.knowledgeCollectionMapper = knowledgeCollectionMapper;
    }

    /**
     * 创建数据定义
     * 
     * @param dataDefinitionDto 数据定义DTO
     * @return 是否创建成功
     */
    @Override
    public boolean createDataDefinition(DataDefinitionDto dataDefinitionDto) {
        CollectionClientInterface client = this.getClient(dataDefinitionDto.getBusinessType());
        return client.createCollection(dataDefinitionDto);
    }

    /**
     * 更新数据定义
     * 
     * @param dataDefinitionDto 数据定义DTO
     * @return 是否更新成功
     */
    @Override
    public boolean updateDataDefinition(DataDefinitionDto dataDefinitionDto) {
        // TODO 更新数据定义
        return false;
    }

    /**
     * 删除数据定义
     * 
     * @param dataDefinitionDto 数据定义DTO
     * @return 是否删除成功
     */
    @Override
    public boolean deleteDataDefinition(DataDefinitionDto dataDefinitionDto) {
        CollectionClientInterface client = this.getClient(dataDefinitionDto.getBusinessType());
        return client.deleteCollection(dataDefinitionDto);
    }

    /**
     * 添加元数据关联
     * 
     * @param dataDefinitionLinkDto 数据定义关联DTO
     * @return 是否添加成功
     */
    @Override
    public boolean addLink(DataDefinitionLinkDto link) {
        // 校验关联
        checkLink(link);

        LambdaQueryWrapper<CollectionMetaLink> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CollectionMetaLink::getMainId, link.getMainId())
                .eq(CollectionMetaLink::getMetaId, link.getMainId());

        CollectionMetaLink _collectionLink = collectionMetaLinkMapper.selectOne(queryWrapper);
        if (_collectionLink != null) {
            throw new IllegalArgumentException("关联已存在");
        }

        CollectionMetaLink collectionLink = HubBeanUtils.convert(link, CollectionMetaLink.class);
        collectionLink.setId(IdGenerator.generateId());
        return collectionMetaLinkMapper.insert(collectionLink) > 0;
    }

    /**
     * 删除元数据关联
     * 
     * @param dataDefinitionLinkDto 数据定义链接DTO
     * @return 是否删除成功
     */
    @Override
    public boolean removeLink(DataDefinitionLinkDto link) {
        // 校验链接
        checkLink(link);

        CollectionMetaLink collectionLink = collectionMetaLinkMapper.selectById(link.getId());
        if (collectionLink == null) {
            throw new IllegalArgumentException("关联不存在");
        }

        return collectionMetaLinkMapper.deleteById(link.getId()) > 0;
    }

    /**
     * 校验关联
     * 
     * @param link
     */
    private void checkLink(DataDefinitionLinkDto link) {
        KnowledgeCollection main = knowledgeCollectionMapper.selectById(link.getMainId());
        KnowledgeCollection meta = knowledgeCollectionMapper.selectById(link.getMetaId());
        if (main == null) {
            throw new IllegalArgumentException("主数据知识库表不存在");
        }
        if (meta == null) {
            throw new IllegalArgumentException("元数据知识库表不存在");
        }
        if (main.getBusinessType() != CollectionTypeEnums.MAIN_DATA.getCode() ||
                meta.getBusinessType() != CollectionTypeEnums.META_DATA.getCode()) {
            throw new IllegalArgumentException("业务类型错误");
        }
    }

    /**
     * 获取元数据关联设置
     * 
     * @param mainCollection 主数据表ID
     * @return 元数据关联设置
     */
    @Override
    public List<Long> getLinks(Long mainCollection) {

        List<CollectionMetaLink> collectionMetaLinks = collectionMetaLinkMapper.selectList(
                new LambdaQueryWrapper<CollectionMetaLink>()
                        .eq(CollectionMetaLink::getMainId, mainCollection));
        return collectionMetaLinks.stream()
                .map(CollectionMetaLink::getMetaId)
                .collect(Collectors.toList());
    }

}
